﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Miris.HumanMilkAnalyser.DataModel;

namespace Miris.HumanMilkAnalyser.Service.Validation
{
    public class PatientValidator : DataValidatorBase<Patient>
    {
        public const string NumberErrorKey = "PatientNumber";
        public const string MotherNameErrorKey = "MotherName";
        public const string MotherAgeErrorKey = "MotherAge";
        public const string MotherHeightErrorKey = "MotherHeight";
        public const string MotherWeightErrorKey = "MotherWeight";
        public const string MotherEmotionErrorKey = "MotherEmotion";
        public const string MotherBadHabitErrorKey = "MotherBadHabit";
        public const string BabyNameErrorKey = "BabyName";
        public const string BabyHeightErrorKey = "BabyHeight";
        public const string BabyWeightErrorKey = "BabyWeight";
        public const string BabyHeadCircumferenceErrorKey = "BabyHeadCircumference";
        public const string BabyBirthHeightErrorKey = "BabyBirthHeight";
        public const string BabyBirthWeightErrorKey = "BabyBirthWeight";
        public const string BabyBirthHeadCircumferenceErrorKey = "BabyBirthHeadCircumference";

        /// <summary>
        /// Validate the provided entity. Then check the IsValid property to see the result.
        /// </summary>
        /// <param name="entity">The provided entity to be validated.</param>
        public override void Validate(Patient entity)
        {
            CheckRequiredNumber(entity);
            CheckNumberLength(entity);
            CheckRequiredMotherName(entity);
            CheckMotherNameLength(entity);
            CheckMotherAge(entity);
            CheckMotherHeight(entity);
            CheckMotherWeight(entity);
            CheckRequiredBabyName(entity);
            CheckBabyNameLength(entity);
            CheckBabyHeight(entity);
            CheckBabyWeight(entity);
            CheckBabyHeadCircumference(entity);
            CheckBabyBirthHeight(entity);
            CheckBabyBirthWeight(entity);
            CheckBabyBirthHeadCircumference(entity);
        }

        /// <summary>
        /// Check if the required number is provided.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckRequiredNumber(Patient entity)
        {
            if (!ValidationHelper.RuleCheckRequiredContent(entity.Number))
            {
                AddError(NumberErrorKey, "受测者ID不能为空");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the number's max length.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckNumberLength(Patient entity)
        {
            int maxLength = 50;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.Number))
            {
                AddError(NumberErrorKey, string.Format("受测者ID长度不能超过{0}", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check if the required mother name is provided.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckRequiredMotherName(Patient entity)
        {
            if (!ValidationHelper.RuleCheckRequiredContent(entity.MotherName))
            {
                AddError(MotherNameErrorKey, "母亲姓名不能为空");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the mother name's max length.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckMotherNameLength(Patient entity)
        {
            int maxLength = 10;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.MotherName))
            {
                AddError(MotherNameErrorKey, string.Format("母亲姓名长度不能超过{0}", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the mother name's age.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckMotherAge(Patient entity)
        {
            int minVale = 16;

            if (entity.MotherAge.HasValue && entity.MotherAge.Value <= minVale)
            {
                AddError(MotherAgeErrorKey, string.Format("母亲年龄不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the mother name's height.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckMotherHeight(Patient entity)
        {
            int minVale = 0;

            if (entity.MotherHeight.HasValue && entity.MotherHeight.Value <= minVale)
            {
                AddError(MotherHeightErrorKey, string.Format("母亲身高不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the mother name's weight.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckMotherWeight(Patient entity)
        {
            int minVale = 0;

            if (entity.MotherWeight.HasValue && entity.MotherWeight.Value <= minVale)
            {
                AddError(MotherWeightErrorKey, string.Format("母亲体重不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the mother emotion length.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckMotherEmotionLength(Patient entity)
        {
            int maxLength = 100;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.MotherEmotion))
            {
                AddError(MotherEmotionErrorKey, string.Format("母亲情绪长度不能超过{0}", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the mother name's bad habit length.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckMotherBadHabitLength(Patient entity)
        {
            int maxLength = 100;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.MotherBadHabit))
            {
                AddError(MotherBadHabitErrorKey, string.Format("母亲不良习惯长度不能超过{0}", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check if the required baby name is provided.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckRequiredBabyName(Patient entity)
        {
            if (!ValidationHelper.RuleCheckRequiredContent(entity.BabyName))
            {
                AddError(BabyNameErrorKey, "婴儿姓名不能为空");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the baby name's max length.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckBabyNameLength(Patient entity)
        {
            int maxLength = 10;

            if (!ValidationHelper.RuleCheckMaxLength(maxLength, entity.BabyName))
            {
                AddError(BabyNameErrorKey, string.Format("婴儿姓名长度不能超过{0}", maxLength));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the baby name's height.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckBabyHeight(Patient entity)
        {
            int minVale = 0;

            if (entity.BabyHeight.HasValue && entity.BabyHeight.Value <= minVale)
            {
                AddError(BabyHeightErrorKey, string.Format("婴儿身长不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the baby name's weight.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckBabyWeight(Patient entity)
        {
            int minVale = 0;

            if (entity.BabyWeight.HasValue && entity.BabyWeight.Value <= minVale)
            {
                AddError(BabyWeightErrorKey, string.Format("婴儿体重不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the baby name's head circumference.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckBabyHeadCircumference(Patient entity)
        {
            int minVale = 0;

            if (entity.BabyHeadCircumference.HasValue && entity.BabyHeadCircumference.Value <= minVale)
            {
                AddError(BabyHeadCircumferenceErrorKey, string.Format("婴儿头围不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the baby name's birth height.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckBabyBirthHeight(Patient entity)
        {
            int minVale = 0;

            if (entity.BabyBirthHeight.HasValue && entity.BabyBirthHeight.Value <= minVale)
            {
                AddError(BabyBirthHeightErrorKey, string.Format("婴儿出生身长不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the baby name's birth weight.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckBabyBirthWeight(Patient entity)
        {
            int minVale = 0;

            if (entity.BabyBirthWeight.HasValue && entity.BabyBirthWeight.Value <= minVale)
            {
                AddError(BabyBirthWeightErrorKey, string.Format("婴儿出生体重不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check the baby name's birth head circumference.
        /// </summary>
        /// <param name="entity">The instance of Patient.</param>
        /// <returns>True means passed. False means failed.</returns>
        public bool CheckBabyBirthHeadCircumference(Patient entity)
        {
            int minVale = 0;

            if (entity.BabyBirthHeadCircumference.HasValue && entity.BabyBirthHeadCircumference.Value <= minVale)
            {
                AddError(BabyBirthHeadCircumferenceErrorKey, string.Format("婴儿出生头围不能小于等于{0}", minVale));
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
